package com.edushare.common.dao;

import java.util.Date;

import com.edushare.common.constants.ConfigPropertyName;
import com.edushare.common.global.SystemGlobals;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.util.DateTimeUtil;
import com.edushare.common.vo.BugReports;
import com.edushare.common.vo.OutgoingEmails;

public class BugReportsDAO extends CayenneBasedDAO {

	public BugReportsDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}

	public void createBugReport(BugReports bug) {
		bug.setTimestamp(new Date());
		getDataContext().registerNewObject(bug);
		emailBugReport(bug);
	}

	public void emailBugReport(BugReports bug) {
		OutgoingEmails mail = new OutgoingEmails();
		mail.setReceiverEmailId(SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.SUPPORT_FEEDBACK_EMAIL));
		mail.setCopyType(OutgoingEmails.COPY_TYPE_TO);
		mail.setStatus(OutgoingEmails.EMAIL_TO_BE_SENT);
		mail.setSubject("[FEEDBACK: "+bug.getReportType()+"] End user feedback.");
		mail.setContent(createContent(bug));
		new OutgoingEmailsDAO(ormInterface).sendMail(mail);
	}

	private String createContent(BugReports bug) {
		String content="" +
				"<table style='width:550px;'>\n" +
					"<tr style='background-color:#DEDDEF;'>\n" +
						"<td style='width:100px;'>\n" +
							"<strong>Report</strong>\n" +
						"</td>\n" +
						"<td style='400px'>\n" +
							"<strong>"+bug.getReportType()+"</strong><BR />\n" +
						"</td>\n" +
					"</tr>\n" +
					"<tr style='background-color:#FFE;'>\n" +
						"<td>\n" +
							"<strong>Description</strong>\n" +
						"</td>\n" +
						"<td>\n" +
							(bug.getReportDesc()+"").replace("\r\n", "<br />").replace("\r", "<br />").replace("\n", "<br />")+"<BR />\n" +
						"</td>\n" +
					"</tr>\n" +
					"<tr style='background-color:#DEDDEF;'>\n" +
						"<td>\n" +
							"<strong>Submitter</strong>\n" +
						"</td>\n" +
						"<td>\n" +
							bug.getReporterName()+"("+bug.getReporterEmail()+")<BR />\n" +
							DateTimeUtil.formatTimestamp(bug.getTimestamp()) +"<BR />\n" +
						"</td>\n" +
					"</tr>\n" +
				"</table>";
		
		return content;
	}

}
